package yag.battle.pet.dao;

import com.mysql.jdbc.Statement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import yag.battle.pet.model.Equipment;
import yag.battle.pet.model.EquipmentMapper;
import yag.battle.pet.model.Pet;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by user on 31.08.2014.
 */
@Repository
public class EquipmentDao {

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }

    public int create(Equipment equipment) {
        KeyHolder holder = new GeneratedKeyHolder();
        String colName = "name";
        final String sql = "insert into equipment (name) values (?) ";
        final Equipment eq = equipment;
        jdbcTemplateObject.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection)
                    throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, eq.getName());
                return ps;
            }
        }, holder);
        int id = holder.getKey().intValue();
        return id;
    }

    public Equipment getById(int id) {
        String SQL = "select * from equipment where id = ?";
        Equipment eq = jdbcTemplateObject.queryForObject(SQL,
                new Object[]{id}, new EquipmentMapper());
        return eq;
    }

    public Equipment getByPetId(int id) {
        try {


            String SQL = "select * from equipment where id_pet = ?";
            Equipment eq = jdbcTemplateObject.queryForObject(SQL,
                    new Object[]{id}, new EquipmentMapper());
            return eq;
        } catch (Exception e) {
            return null;
        }

    }

    public List<Equipment> getList() {
        String SQL = "select * from equipment";
        List<Equipment> eq = jdbcTemplateObject.query(SQL,
                new EquipmentMapper());
        return eq;
    }

    public void updateNameById(int id, String name) {
        String SQL = "update equipment set \"name\" = ? where id = ?";
        jdbcTemplateObject.update(SQL, name, id);
        return;
    }

    public void updateIdPetById(int id, int idPet) {
        String SQL = "update equipment set id_pet = ? where id = ?";
        jdbcTemplateObject.update(SQL, idPet, id);
        return;
    }

    public void deleteById(int id) {
        String SQL = "delete from equipment where id = ?";
        jdbcTemplateObject.update(SQL, id);
        return;
    }

    public void deleteByPetId(int id) {
        String SQL = "delete from equipment where id_pet = ?";
        jdbcTemplateObject.update(SQL, id);
        return;
    }

    public void deletePetIdById(int id) {
        String SQL = "update equipment set id_pet = NULL where id = ?";
        jdbcTemplateObject.update(SQL, id);
        return;

    }

}
